package org.hackystat.ambientdevice.trigger;

import org.hackystat.ambientdevice.ConfigurationException;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.Trigger;
import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
import org.hackystat.sensorbase.client.SensorBaseClient;

/**
 * Factory that produce Validatable object.
 * 
 * @author Shaoxuan Zhang
 */
public class TriggerFactory {

  private SensorBaseClient sensorBaseClient;
  private DailyProjectDataClient dailyProjectDataClient;
  
  /** enumeration of trigger types. */
  enum TRIGGER_TYPE {
    SensorData, Coverage
  }
  
  /**
   * 
   * @param sensorBaseClient the sensor Base client
   * @param dpdClient the DPD client
   */
  public TriggerFactory(SensorBaseClient sensorBaseClient, DailyProjectDataClient dpdClient) {
    this.setSensorBaseClient(sensorBaseClient);
    this.setDailyProjectDataClient(dpdClient);
  }
  
  /**
   * Return a subclass of HackystatTrigger according to the given trigger.
   * 
   * @param trigger the given trigger
   * @param project the project 
   * @return a subclass object of HackystatTrigger
   * @throws ConfigurationException if the trigger is not valid
   */
  public HackystatTrigger getTrigger(Project project, Trigger trigger) 
    throws ConfigurationException {
    TRIGGER_TYPE type = TRIGGER_TYPE.valueOf(trigger.getType());
    switch (type) {
      case SensorData:
        return new SensorDataTrigger(project, trigger, 
            getSensorBaseClient());
      case Coverage:
        return new CoverageTrigger(project, trigger, getDailyProjectDataClient());
      default:
        throw new ConfigurationException("Trigger type " + type.toString() + 
            "is unsupported.");
    }
  }

  /**
   * @param sensorBaseClient the sensorBaseClient to set
   */
  public final void setSensorBaseClient(SensorBaseClient sensorBaseClient) {
    this.sensorBaseClient = sensorBaseClient;
  }

  /**
   * @return the sensorBaseClient.
   */
  public final SensorBaseClient getSensorBaseClient() {
    return sensorBaseClient;
  }

  /**
   * @param dailyProjectDataClient the dailyProjectDataClient to set
   */
  public final void setDailyProjectDataClient(DailyProjectDataClient dailyProjectDataClient) {
    this.dailyProjectDataClient = dailyProjectDataClient;
  }

  /**
   * @return the dailyProjectDataClient
   */
  public final DailyProjectDataClient getDailyProjectDataClient() {
    return dailyProjectDataClient;
  }
}
